/* eslint-disable @typescript-eslint/no-unused-vars */
import moment from 'moment';
import _ from 'lodash';

export const COLUMN = [
  {
    name: '点位',
    unit: '',
    code: 'point',
    width: 80,
    align: 'center',
  },
  {
    name: '调整比例(%)',
    unit: '',
    code: 'scale',
    width: 80,
    align: 'center',
  },
];

export const COLUMNDATA = [
  {
    name: '类型',
    unit: '',
    code: 'type',
    width: 100,
    align: 'center',
  },
];

export const detailData = (data) => {
  const changePoint = data.map((item, index) => {
    return {
      id: `A-${index + 1}`,
      point: `第${index + 1}点`,
      scale: Number(item * 100).toFixed(3),
    };
  });
  return changePoint;
};

//获取两个日期间所有日期
export function getDayAll(_stime, etime) {
  let stime = _stime;
  //初始化日期列表，数组
  var diffdate = new Array();
  var i = 0;
  //开始日期小于等于结束日期,并循环
  while (stime <= etime) {
    diffdate[i] = stime;
    //获取开始日期时间戳
    var stime_ts = new Date(stime).getTime();
    //增加一天时间戳后的日期
    var next_date = stime_ts + 24 * 60 * 60 * 1000;

    //拼接年月日，这里的月份会返回（0-11），所以要+1
    var next_dates_y = new Date(next_date).getFullYear() + '-';
    var next_dates_m =
      new Date(next_date).getMonth() + 1 < 10
        ? '0' + (new Date(next_date).getMonth() + 1) + '-'
        : new Date(next_date).getMonth() + 1 + '-';
    var next_dates_d =
      new Date(next_date).getDate() < 10
        ? '0' + new Date(next_date).getDate()
        : new Date(next_date).getDate();

    stime = next_dates_y + next_dates_m + next_dates_d;
    //增加数组key
    i++;
  }
  return diffdate;
}

export const onTimeList = (startTime, endTime, linght, scaleArr) => {
  let s = moment(startTime).startOf('date');
  let e = moment(endTime).endOf('date');
  const diffNumber = e.diff(s, 'days');
  const timeArr = new Array((diffNumber + 1) * 96);
  const timeFormat = 'YYYYMMDD HHmm';
  const timeFormats = 'YYYY.MM.DD HH:mm';
  let columnArr = [
    {
      name: '类型',
      unit: '',
      code: 'typeLink',
      width: 100,
      lock: true,
      align: 'center',
    },
  ];
  _.forEach(timeArr, () => {
    let obj = { unit: '', width: 80, align: 'center' };
    s.add(15, 'minutes');
    const isDel = s.format('HH:mm') === '00:00';
    const tK = isDel
      ? `${moment(s.valueOf()).add(-1, 'days').format('YYYYMMDD')} 2400`
      : s.format(timeFormat);
    const tF = isDel
      ? `${moment(s.valueOf()).add(-1, 'days').format('YYYY.MM.DD')} 24:00`
      : s.format(timeFormats);
    obj.name = tK;
    obj.code = tF;
    if (String(tF) === linght) {
      obj.getCellProps = () => {
        return { style: { background: 'rgba(255, 255, 255, 0.1)' } };
      };
    }
    obj.render = (text, record) => {
      if (record.id === 'adjustScale') {
        return text || text === 0 ? text : '-';
      }
      return text || text === 0 ? Number(text || 0.0).toFixed(3) : '-';
    };
    columnArr.push(obj);
  });
  return columnArr;
};

export const detailDataList = (data, scaleStr) => {
  let afterData = {},
    beforData = {},
    scalePoint = {},
    dateStr = '';

  Object.entries(data).forEach(([k, i]) => {
    if (k.includes('高亮')) {
      afterData[k] = i;
      if (k === '高亮') {
        Object.entries(i.data).map(([m, n]) => {
          dateStr = `${m} ${n[0].xAxis}`;
        });
      }
    } else {
      beforData[k] = i;
    }
  });
  let adjustmentObj = {},
    dataObj = {},
    summary = {};
  Object.entries(afterData).map(([k, i]) => {
    if (k === '高亮') {
      const keys = Object.keys(i.data)[0],
        values = Object.values(i.data)[0];
      dataObj[keys] = [...(dataObj?.[keys] || []), ...values];
    } else {
      Object.entries(i.data).map(([m, n]) => {
        if (dateStr.includes(m)) {
          dataObj[m] = [...(dataObj?.[m] || []), ...n];
        } else {
          adjustmentObj[m] = [...(dataObj?.[m] || []), ...n];
        }
      });
    }
    summary = i;
  });
  summary = {
    ...summary,
    data: { ...dataObj, ...adjustmentObj },
  };
  let dataSourceList = detailDataArr(beforData);
  // const [realTime = {}] = dataSourceList;
  let obj = {};
  dataSourceList.forEach((item) => {
    if (item.typeLink === '比例') {
      Object.keys(item).forEach((key) => (obj[key] = item[key] || 0));
    }
  });
  const adjustScale = { ...obj, id: 'adjustScale', isSum: false, typeLink: '调整比例' };
  const summaryList = detailDataArr({ 调整后: summary }, true);
  // const scalePointObj=detailDataArr({ 调整比例: scalePoint }, false)
  const [datas, dataT] = dataSourceList;
  if (datas?.id.includes('第')) {
    dataSourceList = dataSourceList.reverse();
  }
  return {
    dataSourceList: [...dataSourceList, ...summaryList, adjustScale],
    dateStr: dateStr,
  };
};
const detailDataArr = (beforData, isAfter = false) => {
  let dataSourceList = [];
  if (Object.values(beforData).length === 0) return dataSourceList;
  Object.entries(beforData).map(([k, i]) => {
    let obj = { typeLink: k, id: k, color: i.color, type: i.type, isSum: isAfter };
    Object.entries(i.data).map(([ks, is]) => {
      is.map((m) => {
        obj[`${ks} ${m.xAxis}`] = m.value;
      });
    });
    dataSourceList.push(obj);
  });
  return dataSourceList;
};

export const scaleDataList = (
  data,
  startTime,
  endTime,
  highLight = moment().format('YYYY.MM.DD HH:mm'),
  scale = 1,
) => {
  let s = moment(startTime).startOf('date');
  // let e = moment(endTime).endOf('date');
  const n = moment();
  const hasData = n.endOf('date').diff(n, 'minutes') <= 239 && s.diff(n, 'days') === 1;
  // const [before, after] = data.filter(i => {
  //   return i?.id?.includes('第') || i?.id?.includes('调整后');
  // });
  const dataSource = data
    .map((o) => {
      let obj = {};
      if (o?.id === 'adjustScale') {
        Object.entries(o).map(([ks]) => {
          const isDel = _.replace(ks, '24:00', '23:59');
          if (moment(isDel).diff(moment(highLight), 'minutes') > 0 || hasData) {
            const scaleValue = Number(scale * 100).toFixed(3);
            obj[ks] = `${!isNaN(scaleValue) && scaleValue ? scaleValue : '-'}%`;
          } else {
            // const scaleValue = after?.[ks] || after?.[ks] === 0 || before?.[ks] || before?.[ks] === 0 ? Number((after?.[ks] / before?.[ks]) * 100).toFixed(3) : '-';
            const scaleValue = o?.[ks] && o?.[ks] !== 0 ? Number(o?.[ks] * 100).toFixed(3) : '-';
            obj[ks] = `${scaleValue && scaleValue !== '-' ? `${scaleValue}%` : '-'}`;
          }
        });
        obj.typeLink = o.typeLink;
        obj.id = o?.id;
      } else if (o.isSum) {
        Object.entries(o).map(([ks, is]) => {
          const isDel = _.replace(ks, '24:00', '23:59');
          if (
            (moment(isDel).diff(moment(highLight), 'minutes') > 0 || hasData) &&
            (is || is === 0)
          ) {
            obj[ks] = (is * scale).toFixed(3);
          } else {
            obj[ks] = is;
          }
        });
      } else {
        obj = { ...o };
      }
      return obj;
    })
    ?.filter((v) => v.typeLink !== '比例');
  return dataSource;
};
